include([],

function() {

	declare('controller.Button');
	
	Button = function(hitregion, gamewindow){
		this.touches = [];
		this.gamewindow = gamewindow;
		this.hitregion = hitregion;
		this.listeners = [];
		
		this.size = 20;
	}
	
	Button.prototype = {
			inputChanged : function(){
				var touches = this.touches;
				iterate(this.listeners, function(callback){
					callback(touches);
				});
			},	
			addListener : function(callback){
				this.listeners.push(callback);
			},	
		draw : function(context2d){
			var c = context2d;
	
			for(var i=0; i < this.touches.length;i++){
				if(!this.touches[i]) continue;
				
				var e = this.touches[i];
				
				c.beginPath(); 
				c.fillStyle = "white";
				c.fillText("touch id : "+e.bId.id+" x:"+e.x+" y:"+e.y, e.x+30, e.y-30); 
		
				c.beginPath(); 
				c.strokeStyle = "red";
				c.lineWidth = "3";
				c.arc(e.x, e.y, this.size, 0, Math.PI*2, true); 
				c.stroke();
			}
		},
		onButtonDown : function(e) {
			if( this.hitregion.intersects(e) )
			{
				this.touches.push(e);
				this.inputChanged();
			}
		},
		onMove : function (e) {
			if(e.bId && this.touches.length > 0 )
			{
				var id = this.touches.indexOf(e);
				if(id>=0) this.touches[id] = e;
				
				//this.inputChanged();
			}
		}, 
		onButtonUp : function (e) { 
			if(e.bId && this.touches.length > 0)
			{
				var id = this.touches.indexOf(e);
				if(id>=0){
					this.touches = this.touches.remove(id);
				}
				
				//this.inputChanged();
			}		
		}
		
	}
});